package com.qianfeng.smsplatform.webmaster.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qianfeng.smsplatform.common.constants.CacheConstants;
import com.qianfeng.smsplatform.webmaster.dao.TDirtywordMapper;
import com.qianfeng.smsplatform.webmaster.dto.DataGridResult;
import com.qianfeng.smsplatform.webmaster.dto.QueryDTO;
import com.qianfeng.smsplatform.webmaster.mq.PushDirtyWordsCacheChange;
import com.qianfeng.smsplatform.webmaster.pojo.TDirtyword;
import com.qianfeng.smsplatform.webmaster.pojo.TDirtywordExample;
import com.qianfeng.smsplatform.webmaster.service.DirtywordService;
import com.qianfeng.smsplatform.webmaster.service.api.CacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DirtywordServiceImpl implements DirtywordService {

    @Autowired
    private TDirtywordMapper tDirtywordMapper;

    @Autowired
    private CacheService cacheService;

    @Autowired
    PushDirtyWordsCacheChange pushDirtyWordsCacheChange;

    @Override
    public int addDirtyword(TDirtyword tDirtyword) {
//        cacheService.saveCache("DIRTYWORDS:" + tDirtyword.getDirtyword(), "1");

        cacheService.add2Set(CacheConstants.CACHE_DIRTY_KEY, Arrays.asList(tDirtyword.getDirtyword()));

        pushDirtyWordsCacheChange.updateBlackChannel().send(new GenericMessage<>("1"));

        return tDirtywordMapper.insertSelective(tDirtyword);
    }

    @Override
    public int delDirtyword(Long id) {

        TDirtyword tDirtyword = findById(id);

        cacheService.deleteCache2Set(CacheConstants.CACHE_DIRTY_KEY, Arrays.asList(tDirtyword.getDirtyword()));

        pushDirtyWordsCacheChange.updateBlackChannel().send(new GenericMessage<>("1"));

        return tDirtywordMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int updateDirtyword(TDirtyword tDirtyword) {

        TDirtyword tDirtyword1 = tDirtywordMapper.selectByPrimaryKey(tDirtyword.getId());

        int i = tDirtywordMapper.updateByPrimaryKey(tDirtyword);

        if (i > 0) {

            cacheService.deleteCache2Set(CacheConstants.CACHE_DIRTY_KEY,Arrays.asList(tDirtyword1.getDirtyword()));
            cacheService.save2Cache2Set(CacheConstants.CACHE_DIRTY_KEY,Arrays.asList(tDirtyword.getDirtyword()));

            //通知到 MQ,更新本地缓存
            pushDirtyWordsCacheChange.updateBlackChannel().send(new GenericMessage<>("1"));

        }
        return i;
    }

    @Override
    public TDirtyword findById(Long id) {
        return tDirtywordMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<TDirtyword> findAll() {
        return tDirtywordMapper.selectByExample(null);
    }

    @Override
    public DataGridResult findByPage(QueryDTO queryDTO) {
        PageHelper.offsetPage(queryDTO.getOffset(), queryDTO.getLimit());
        TDirtywordExample example = new TDirtywordExample();
        String sort = queryDTO.getSort();
        if (!StringUtils.isEmpty(sort)) {
            example.setOrderByClause("id");
        }
        List<TDirtyword> tDirtywords = tDirtywordMapper.selectByExample(example);
        PageInfo<TDirtyword> info = new PageInfo<>(tDirtywords);
        long total = info.getTotal();
        DataGridResult result = new DataGridResult(total, tDirtywords);
        return result;
    }

    @Override
    public void syncDirtyword2Redis() {
        //获取所有的数据
        //通过通道操作保存到redis
        TDirtywordExample example = new TDirtywordExample();
        List<TDirtyword> tDirtywordList = tDirtywordMapper.selectByExample(example);//获取所有的数据
//        HashMap<Object, Object> hashMap = new HashMap<>();
//        for (TDirtyword tDirtyword : tDirtywordList) {
//            String dirtyword = tDirtyword.getDirtyword();
//            hashMap.put(CacheConstants.CACHE_PREFIX_DIRTYWORDS + dirtyword, "1");
//        }

        //以下代码使用的是 每个敏感词单独key的方式保存
//        Map<String, Object> map = tDirtywordList.stream().map(tDirtyword -> CacheConstants.CACHE_PREFIX_DIRTYWORDS + tDirtyword.getDirtyword())
//                .collect(Collectors.toMap(String -> String, String::length));
//        System.err.println(map);
//        cacheService.pipelineOps(map);

        cacheService.del(CacheConstants.CACHE_DIRTY_KEY);
        List<String> numList = tDirtywordList.stream().map(TDirtyword::getDirtyword).collect(Collectors.toList());
        cacheService.add2Set(CacheConstants.CACHE_DIRTY_KEY,numList);

    }


}
